package com.hznc.wcs.controller.bpm;

import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Pagination;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.diboot.iam.annotation.OperationCons;
import com.hznc.wcs.service.bpm.BpmProcessTaskService;
import com.hznc.wcs.vo.bpm.*;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.UserTask;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description : 流程任务接口
 * @Date : 2024/6/20
 * @Author : ChenJin
 * @Version : v1.0.0
 **/
@Slf4j
@RestController
@BindPermission(name = "流程任务",code = "BpmTask")
@RequestMapping("/bpm-task")
public class BpmTaskController {

    @Resource
    private BpmProcessTaskService bpmProcessTaskService;

    /**
     * 查询我的待办
     * @param reqVO
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/todoPage")
    public JsonResult<?> getTodoPage(BpmTaskReqVO reqVO, Pagination pagination){
        List<BpmTaskRespVO> todoList = bpmProcessTaskService.getTodoList(reqVO, pagination);
        return JsonResult.OK(todoList).bindPagination(pagination);
    }

    /**
     * 查询我的已办
     * @param reqVO
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/donePage")
    public JsonResult<?> getDonePage(BpmTaskReqVO reqVO, Pagination pagination){
        List<BpmTaskRespVO> doneList = bpmProcessTaskService.getDoneList(reqVO, pagination);
        return JsonResult.OK(doneList).bindPagination(pagination);
    }

    /**
     * 审批通过
     * @param reqVO
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/approveTask")
    public JsonResult<?> approveTask(@Valid @RequestBody BpmTaskApproveReqVO reqVO){
        bpmProcessTaskService.approveTask(reqVO);
        return JsonResult.OK();
    }

    /**
     * 审批不通过
     * @param reqVO
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/rejectTask")
    public JsonResult<?> rejectTask(@Valid @RequestBody BpmTaskRejectReqVO reqVO) {
        bpmProcessTaskService.rejectTask(reqVO);
        return JsonResult.OK();
    }

    /**
     * 获取可回退的任务节点
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/getReturnTaskList")
    public JsonResult<?> getReturnTaskList(@RequestParam("id") String id) {
        List<UserTask> returnTaskList = bpmProcessTaskService.getReturnTaskList(id);
        return JsonResult.OK(returnTaskList);
    }

    /**
     * 回退任务
     * @param reqVO
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/returnTask")
    public JsonResult<?> returnTask(@Valid @RequestBody BpmTaskReturnReqVO reqVO) {
        bpmProcessTaskService.returnTask(reqVO);
        return JsonResult.OK();
    }

    /**
     * 任务委派
     * @param reqVO
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/delegateTask")
    public JsonResult<?> delegateTask(@Valid @RequestBody BpmTaskDelegateReqVO reqVO) {
        bpmProcessTaskService.delegateTask( reqVO);
        return JsonResult.OK();
    }

    /**
     * 任务转派
     * @param reqVO
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/transferTask")
    public JsonResult<?> transferTask(@Valid @RequestBody BpmTaskTransferReqVO reqVO) {
        bpmProcessTaskService.transferTask(reqVO);
        return JsonResult.OK();
    }

    /**
     * 加签
     * @param reqVO
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/addSignTask")
    public JsonResult<?> addSignTask(@Valid @RequestBody BpmTaskAddSignReqVO reqVO) {
        bpmProcessTaskService.addSignTask(reqVO);
        return JsonResult.OK();
    }

    /**
     * 减签
     * @param reqVO
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/deleteSignTask")
    public JsonResult<?> deleteSignTask(@Valid @RequestBody BpmTaskAddSignReqVO reqVO) {
        bpmProcessTaskService.deleteSignTask(reqVO);
        return JsonResult.OK();
    }

    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @PutMapping("/getDeleteSignTaskList")
    public JsonResult<?> getDeleteSignTaskList(@RequestParam("parentTaskId") String parentTaskId) {
        List<BpmTaskRespVO> deleteSignTaskList = bpmProcessTaskService.getDeleteSignTaskList(parentTaskId);
        return JsonResult.OK(deleteSignTaskList);
    }

}
